from time import time


class Template:

    def __init__(self, result:list,name:str):
        self.data = result
        self.name = name
        self._head()
        self._body()
        self._html()

    def _head(self):
        self.head = f"<head><title>{self.name}</title></head>"

    def _body(self):
        tb = "<tr>" \
             "<td>编号</td>" \
             "<td>测试动作</td>" \
             "<td>动作响应</td>" \
             "<td>pre 截图</td>" \
             "<td>after 截图</td>" \
             "</tr>"
        for i,v in enumerate(self.data):
            tb += "<tr>" \
                  f"<td>{i+1}</td>"\
                  f"<td>{v[0]}</td>"\
                  f"<td>{v[1]}</td>"\
                  f"<td><img src={v[2].split('/')[2]} width='400' height='250'></td>"\
                  f"<td><img src={v[3].split('/')[2]} width='400' height='250'></td>"\
                  "</tr>"
        table = f"<table border='1' align='center'>{tb}</table>"
        self.body = f"<body>{table}</body>"

    def _html(self):
        self.html = f"<html>{self.head}{self.body}</html>"

    def html_to_file(self, file):
        with open(file, 'x',encoding='utf-8') as f:
            f.write(self.html)


class ResultTemplate:
    def __init__(self, result:list):
        self.data = result
        self._head()
        self._body()
        self._html()

    def _head(self):
        self.head = f"<head><title>测试结果汇总</title></head>"

    def _body(self):
        tb = "<tr>" \
             "<td>编号</td>" \
             "<td>测试名称</td>" \
             "<td>测试结果</td>" \
             "<td>详情</td>" \
             "</tr>"
        for i,v in enumerate(self.data):
            tb += "<tr>" \
                  f"<td>{i+1}</td>"\
                  f"<td>{v[0]}</td>"\
                  f"<td>{v[1]}</td>"\
                  f"<td><a href='{v[2]}' target='top'>{v[2]}</a></td>"\
                  "</tr>"
        table = f"<table border='1' align='center'>{tb}</table>"
        self.body = f"<body>{table}</body>"

    def _html(self):
        self.html = f"<html>{self.head}{self.body}</html>"

    def html_to_file(self, file):
        with open(file, 'x',encoding='utf-8') as f:
            f.write(self.html)
